package com.boilerCloud.dao.device;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.DeviceInfo;
import com.boilerCloud.dto.device.DeviceWarnInfo;
import com.boilerCloud.util.StringUtil;
@Repository
public class DeviceWarnDao extends BaseDao {

	public void qryList(DeviceWarnInfo device, HzlqswPage<DeviceWarnInfo> page) throws Exception {
		String countsql = "select count(*) from d_device_info a "
				+ " left join d_model_info b on b.id = a.module_id"
				+ " left join dictionary c on c.id = a.type_id"
				+ " where a.is_delete='n' ";
			
		String listSql = "SELECT b.name deviceName,a.* FROM `d_device_warn_info` a "
 +"LEFT JOIN `d_device_info` b ON a.`device_id` = b.`id`	";
		List<Object> params= new ArrayList<Object>();
		/*if(!StringUtil.isEmpty(device.getDeviceNo())){
			countsql += " and a.device_no like ?";
			listSql += " and a.device_no like ?";
			params.add("%" + device.getDeviceNo() +"%");
		}
		
		if(device.getTypeId() != 0 && device.getTypeId() != -1){
			countsql += " and a.type_id = ?";
			listSql += " and a.type_id = ?";
			params.add(device.getTypeId());
		}		*/
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, DeviceWarnInfo.class);
		
	}

	public void edit(DeviceInfo device) {
		String sql = "update d_device_info set name=?,longitude=?,latitude=?,type_id=?,modify_time=now() where id=?";
		List<Object> param = new ArrayList<Object>();
		//param.add(device.getDeviceNo());
		param.add(device.getName());
		//param.add(device.getProduceTimeStr());
		//.add(device.getModuleId());
		param.add(device.getLongitude());
		param.add(device.getLatitude());
		param.add(device.getTypeId());
		param.add(device.getId());
		this.jdbcTemplate.update(sql,param.toArray());
	}

	public void del(int id) {
		String sql = "update d_device_info set is_delete='y' where id=?";
		this.jdbcTemplate.update(sql,id);
		
	}

	public void add(DeviceInfo device)throws Exception {
		String sql = "insert into d_device_info(device_no,name,produce_time,"
				+ "module_id,longitude,latitude,type_id,create_time,modify_time,is_delete)"
				+ " values(?,?,?,?,?,?,?,now(),now(),'n')";
		/*List<Object> param = new ArrayList<Object>();
	//	param.add(device.getDeviceNo());
		param.add(device.getName());
		//param.add(device.getProduceTimeStr());
		//param.add(device.getModuleId());
		param.add(device.getLongitude());
		param.add(device.getLatitude());
		param.add(device.getTypeId());
		this.jdbcTemplate.update(sql,param.toArray());*/
		
	}

	public DeviceInfo getDeviceInfo(int id)throws Exception {
		String sql = "select * from d_device_info where id = ? limit 1";
		return this.queryForObject(sql, DeviceInfo.class, id);
		
	}

}
